package com.info33.platform.doc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.doc.dto.DocFileDTO;
import com.info33.platform.doc.dto.DocFileSearchDTO;
import com.info33.platform.doc.entity.DocFile;
import com.info33.platform.doc.entity.DocFileDetail;
import com.info33.platform.doc.service.DocFileService;
import com.info33.platform.doc.vo.DocFileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author admin
 */
@RestController
@RequestMapping("/DocFile")
@Api(tags = "文档管理 - 文件信息")
public class DocFileController extends BaseController<DocFileService, DocFile> {

    private final DocFileService docFileService;

    @Autowired
    public DocFileController(DocFileService docFileService) {
        super(docFileService);
        this.docFileService = docFileService;
    }

    @PostMapping(value = "/gw/businessUpload")
    @ApiOperation(value = "门户--多文件上传--业务模块")
    public Result<List<DocFileDetail>> businessUploadByGateWay(
            @RequestParam(value = "file") MultipartFile[] multipartFiles,
            String module) {
        return docFileService.businessUpload(multipartFiles, module, Long.valueOf("1"));
    }

    @PostMapping(value = "/businessUpload", headers="content-type=multipart/form-data")
    @ApiOperation(value = "多文件上传--业务模块")
    public Result<List<DocFileDetail>> businessUpload(@RequestParam(value = "file") MultipartFile[] multipartFiles, String module) {
        return docFileService.businessUpload(multipartFiles, module, getTenantId());
    }

    @PostMapping(value = "/pageDocFileList/{pageNum}/{pageSize}")
    @ApiOperation(value = "分页获取文档列表")
    public Result<IPage<DocFileVO>> pageDocFileList(@RequestBody DocFileSearchDTO docFileSearchDTO, @PathVariable int pageNum, @PathVariable int pageSize) {
        IPage<DocFileVO> page = new Page<>(pageNum, pageSize);
        return Result.ok(docFileService.pageDocFileList(page, docFileSearchDTO, getCurrentUser()));
    }

    @PostMapping(value = "/upload")
    @ApiOperation(value = "多文件上传")
    public Result<?> upload(@RequestParam(value = "file") MultipartFile[] multipartFiles, DocFileDTO docFileDTO) {
        return docFileService.upload(multipartFiles, docFileDTO, getTenantId());
    }

    @GetMapping(value = "/getDetail/{id}")
    @ApiOperation(value = "获取详情")
    public Result<DocFileVO> getDetail(@PathVariable Long id) {
        return Result.ok(docFileService.getDetail(id));
    }

    @PostMapping(value = "/editFile")
    @ApiOperation(value = "编辑文件上传")
    public Result<?> editFile(@RequestParam(value = "file") MultipartFile[] multipartFiles, DocFileDTO docFileDTO) {
        return docFileService.editFile(multipartFiles, docFileDTO, getTenantId());
    }

    @PostMapping(value = "/removeFile")
    @ApiOperation(value = "批量删除")
    public Result<?> removeFile(@RequestBody List<Long> ids) {
        return docFileService.removeFile(ids);
    }

    @GetMapping(value = "/pc/list/{ids}")
    @ApiOperation(value = "pc - 多文件查询", notes = "id 集合，附带对应上传人信息，sys_tenant_user 用户")
    public Result<List<DocFileVO>> pcList(@PathVariable(value = "ids") String ids) {
        return Result.ok(docFileService.listWithUserInfo(ids));
    }


}